﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Darkmoon.DX11Imported;

namespace DX11ForCSharp
{
    class Class1
    {
        public uint _V1;
    }

    [StructLayout(LayoutKind.Explicit)]
    class Struct1
    {
        [FieldOffset(0)]
        public uint _V1;
        [FieldOffset(0)]
        public int _V2;
        [FieldOffset(4)]
        public string _V3;
        [FieldOffset(4)]
        public int[][] _V4;
    }

    [StructLayout(LayoutKind.Sequential)]
    class Struct2
    {
        [MarshalAs(UnmanagedType.LPWStr, SizeConst=5)]
        public string _V1;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct Struct3
    {
        public GCHandle _H1;
    }

    class Class2
    {
        public IntPtr GetSize(GCHandle h1 = default(GCHandle))
        {
            return new IntPtr(100);
        }
    }
    static class Class2Extensions
    {
        public static long GetSizeLong(this Class2 _this)
        {
            return _this.GetSize().ToInt64();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //System.Windows.

            //int[] IntArr1 = new int[12];
            //unsafe
            //{
            //    fixed (int* pInt = IntArr1)
            //    {
            //        ID3D11Device pDevice = null;
            //        //ID3D11DeviceContext pDC = null;
            //        //Darkmoon.DX11Imported.D3D_FEATURE_LEVEL fl = new Darkmoon.DX11Imported.D3D_FEATURE_LEVEL();
            //        LP_IDXGISwapChain ppSwapChain = new LP_IDXGISwapChain();
            //        LP_D3D_FEATURE_LEVEL pFL = new LP_D3D_FEATURE_LEVEL();
            //        LP_ID3D11DeviceContext ppDC = new LP_ID3D11DeviceContext();
            //        LP_ID3D11Device ppDevice = new LP_ID3D11Device();
            //        {
            //            Darkmoon.DX11Imported.DX11ImportLib_GlobalMethods.D3D11CreateDeviceAndSwapChain
            //                (null, Darkmoon.DX11Imported.D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, IntPtr.Zero,
            //                new Darkmoon.DX11Imported.D3D11_CREATE_DEVICE_FLAG(), null, 0, Darkmoon.DX11Imported.DX_SDK_VERSIONS.D3D11_SDK_VERSION, ppSwapChain: ppSwapChain, ppImmediateContext: ppDC, ppDevice: ppDevice);
            //        }
            //        pDevice = (ID3D11Device)ppDevice.Value;
            //        IDXGISwapChain pSwapChain = (IDXGISwapChain)ppSwapChain.Value;
            //        object pDeviceRaw = pSwapChain.GetDevice(DX11_Guids_And_Consts.IID_IDXGIDevice);
            //    }
            //}

            //Struct1 s1 = new Struct1();
            //s1._V1 = 1;
            //s1._V3 = "DSdfgg";
            ////var i1 = s1._V4[0][0];
            
            ////int sz1 = Marshal.SizeOf(s1);

            //Struct2 s2 = new Struct2();
            //s2._V1 = "sdfgdsfgdfhfgh";
            //int sz2 = Marshal.SizeOf(s2);

            //unsafe
            //{
            //    int sz3 = sizeof(bool);
            //}

            //Struct3 s3 = new Struct3();
            //    //s3._H1.Free();
            //char[] str = ("Lalala").ToArray();
            //s3._H1 = GCHandle.Alloc(str, GCHandleType.Normal);

            //unsafe
            //{
            //    char** pptr3 = (char**)(void*)&s3;
            //    char* ptr3 = *pptr3;
            //    char ch1 = ptr3[0];

            //    fixed (char* ptr4 = str)
            //    {
            //        char ch2 = ptr4[0];
            //    }
            //}

            //if (s3._H1.IsAllocated)
            //    s3._H1.Free();

            //Class2 o2 = new Class2();
            //long l1 = o2.GetSizeLong();

            int[,] arr1 = new int[5, 6];
            IntPtr ptr = Marshal.AllocHGlobal(120);
            bool b1 = arr1 is Array;
            GCHandle hArr = GCHandle.Alloc(arr1, GCHandleType.Pinned);
            var ptrArr = hArr.AddrOfPinnedObject();

            unsafe
            {
                fixed (void* ptr2 = arr1)
                {
                }
            }

            int n1 = ((Array)arr1).Length;
            //Marshal.StructureToPtr(arr1, ptr, false);
            int nSize = Marshal.SizeOf(arr1);
        }
    }
}
